package com.fencing_squad.homo.list;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Product {
    String itemId = "";
    String image = "";
    String name = "";
    int point = 0;
    int itemNum = 0;
    int number = 0;

    public Product() {}

    public Product(JSONObject object) {
        itemId = object.getString("item_id");
        name = object.getString("item_name");
        point = object.getIntValue("price");
        itemNum = object.getIntValue("item_num");
        image = object.getString("img");
    }

    public String getItemId() {
        return itemId;
    }

    public void setItemId(String itemId) {
        this.itemId = itemId;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPoint() {
        return String.valueOf(point);
    }

    public int getPointInt() {
        return point;
    }

    public void setPoint(int point) {
        this.point = point;
    }

    public String getItemNum() {
        return String.valueOf(itemNum);
    }

    public void setItemNum(int itemNum) {
        this.itemNum = itemNum;
    }

    public String getNumber() {
        return String.valueOf(number);
    }

    public int getNumberInt() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public int getTotal() {
        return number * point;
    }

    public void addNumber() {
        if (this.number < this.itemNum) {
            this.number += 1;
        }
    }

    public void subNumber() {
        if (this.number > 0) {
            this.number -= 1;
        }
    }

    public static ArrayList<Product> getFromJSONArray(JSONArray array) {
        ArrayList<Product> products = new ArrayList<>();
        for(int i=0; i<array.size(); i++) {
            products.add(new Product(array.getJSONObject(i)));
        }
        return products;
    }
}
